package com.csd.serviceImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.sql.Update;

import com.csd.dao.TeacherDAO;
import com.csd.exception.*;
import com.csd.helper.IConstant;
import com.csd.helper.SessionManager;
import com.csd.pojo.Manager;
import com.csd.pojo.Teacher;
import com.csd.service.ITeacherService;
import com.csd.vo.UserVO;

/**
 * 教师业务逻辑
 * 
 * @author 尹华彬，sanmu <br>
 *         创建于2009-12-17
 */
public class TeacherService implements ITeacherService {

	private TeacherDAO teacherDAO;

	public TeacherDAO getTeacherDAO() {
		return teacherDAO;
	}

	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}

	/**
	 * 老师修改密码
	 * 
	 * @param teacherID
	 *            老师id
	 * @param oldPassword
	 *            原来密码
	 * @param newPassword
	 *            新密码
	 * @return 修改成功返回true
	 */
	public boolean resetPassword(String oldPassword, String newPassword) {
		boolean bl = false;
		try {
			UserVO vo = SessionManager.getUserVO();
			if (vo != null && vo.getType() == IConstant.teacherRole) {
				Teacher teacher = teacherDAO.findById(vo.getUserID());
				if (teacher.getPassword().equals(oldPassword)) {
					teacher.setPassword(newPassword);
					teacherDAO.updateTeacher(teacher);
					bl = true;
				}
			}

		} catch (RuntimeException re) {
			throw new UpdateException(re, "修改密码异常:旧密码:"+oldPassword);
		}
		return bl;
	}
	/**
	 * 老师修改个人信息
	 * 
	 * @param teacherID
	 *            老师id
	 * @param email
	 *            电子邮件
	 * @param phone
	 *            电话
	 * @param website
	 *            个人网站
	 * @param introduction
	 *            个人介绍    
	 * @return true为修改成功，false 为修改失败
	 */
	public boolean updateTeacherInfo(Integer teacherID, String email,
			String phone, String website, String introduction) {
		Teacher teacher = null;
		teacher = teacherDAO.findById(teacherID);
		try {
			if (teacher != null) {
				teacher.setEmail(email);
				teacher.setIntroduction(introduction);
				teacher.setWebsite(website);
				teacher.setPhone(phone);
			}
			teacherDAO.updateTeacher(teacher);
		} catch (RuntimeException re) {
			throw new UpdateException(re, " 修改老师信息异常");
		}
		return true;
	}

	/**
	 * 教师登录
	 * 
	 * @param userVO
	 * @return 
	 */
	public UserVO login(UserVO userVO) {
		UserVO uVO = null;

		try {
			Teacher temp = null;
			List<Teacher> list = null;
			list = teacherDAO.findByUsername(userVO.getLoginName());
			if (!list.isEmpty() && list.size() == 1) {
				temp = list.get(0);
				if (userVO.getPassword().equals(temp.getPassword())) {
					uVO = new UserVO(temp.getId(), temp.getUsername(),
							IConstant.teacherRole, temp.getName());
				}
			}
		} catch (RuntimeException re) {
			throw new RuntimeException("登陆异常");
		}
		return uVO;
	}

	/**
	 * 列出所有老师
	 * 
	 * @return 老师实例列表
	 */
	public List<UserVO> listAllTeacher() {
		List<UserVO> uList = new ArrayList<UserVO>();
		List<Teacher> list = null;

		try {
			list = teacherDAO.findAll();
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Teacher teacher = (Teacher) iterator.next();
				uList.add(new UserVO(teacher.getId(), "", "", teacher.getName()));
			}
		} catch (RuntimeException re) {
			throw new GetException(re, "查询老师异常");
		}

		return uList;

	}

	/**
	 * 根据教师id查找教师实例
	 * 
	 * @param ID
	 *            教师id
	 * @return 教师实例
	 */
	public Teacher getTeacherByID(Integer ID) {
		Teacher teacher = null;

		try {
			teacher = teacherDAO.findById(ID);
		} catch (RuntimeException re) {
			throw new GetException(re, "查询老师信息异常");
		}

		return teacher;
	}

	/**
	 * 列出该项目的评审名单
	 * 
	 * @param projectID
	 *            项目id
	 * @return 教师对象列表
	 */
	public List<Teacher> listEvaluateTeacher(Integer projectID) {
		List<Teacher> tempList = null;
		try {
			List<Object> oList = teacherDAO.listEvaluateTeacher(projectID);
			tempList = (List<Teacher>) oList.get(0);
		} catch (RuntimeException re) {

			throw new GetException(re, "查询项目评审名单异常");
		}
		return tempList;
	}

	/**
	 * 列出可供选择的老师列表
	 * 
	 * @return 教师对象列表
	 */
	public List<Teacher> listSelectTeacher() {
		List<Teacher> tempList = null;
		try {
			List<Object> oList = teacherDAO.listSelectTeacher();
			tempList = (List<Teacher>) oList.get(0);
		} catch (RuntimeException re) {

			throw new GetException(re, "查询可供选择的老师列表异常");
		}
		return tempList;
	}
	
	public List<Teacher> findTeachersExceptInstructor(Integer id) 
	{
		
		List<Teacher> list = null;
		
		try
		{
			list = teacherDAO.findTeachersExceptInstructor(id);
		}
		catch(RuntimeException re)
		{
			throw new GetException(re, "通过项目ID查询老师错误！");
		}
		
		return list;
	}
}
